/**
 * 
 */
package com.gragra.sampling.nonproj.tests;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.nonproj.FixedParsesAssigner;
import com.gragra.sampling.nonproj.HeadFrequenciesFactory;
import com.gragra.sampling.nonproj.generators.CreateDependencyCorpus;
import com.gragra.sampling.nonproj.generators.nonprojective.singleroot.CreateGreedyRandomSingleRootNonProj;
import com.gragra.sampling.nonproj.structures.nonprojective.NonProjDepdTree;
/**
 * @author Christoph Teichmann
 * 22.09.2013
 */
public class TestFixedParsesAssigner
{
	/**
	 * 
	 */
	private NonProjDepdTree	tree;
	/**
	 * 
	 */
	private FixedParsesAssigner	fpa;
	/**
	 * 
	 */
	private ThreadedUniformAccess tua;
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception
	{
		CreateDependencyCorpus cre = new CreateGreedyRandomSingleRootNonProj(new HeadFrequenciesFactory());
		tree = cre.makeTree("this|0 is|1 a|2 sentence|3 .|4",5,0.1);
		ArrayList<String> golds = new ArrayList<String>();
		golds.add("4 5 0 4 1");
		golds.add("3 4 4 5 0");
		fpa = new FixedParsesAssigner(5, 10.0, 3, golds);
		tua = new ThreadedUniformAccess(5);
		tree.initialize(fpa, tua);
	}
	@Test
	public void testCounters()
	{
		for(int i=0;i<10;++i)
		{
			for(int thread=0;thread<5;++thread)
			{tree.resample(fpa, 1.0, thread, tua);}
		}
		assertEquals(fpa.getNumberOfAuxEvals(),10000.0,1000.0);
		assertEquals(fpa.getNumberOfEvaluations(),4000.0,500.0);
	}
	/**
	 * Test method for {@link com.gragra.sampling.nonproj.FixedParsesAssigner#getPopulationSize()}.
	 */
	@Test
	public void testGetPopulationSize()
	{assertEquals(fpa.getPopulationSize(),5);}
	/**
	 * Test method for {@link com.gragra.sampling.nonproj.FixedParsesAssigner#makeLogProb(com.gragra.sampling.nonproj.NonProjDepdTree, int, int)}.
	 */
	@Test
	public void testMakeLogProb()
	{
		for(int i=0;i<1000;++i)
		{
			for(int thread=0;thread<5;++thread)
			{tree.resample(fpa, 1.0, thread, tua);}
			tree.addSample();
		}
		assertEquals(tree.getNumberSeen("4 5 0 4 1")/tree.getNumberSeen("3 4 4 5 0"),1.0,5.0);
		assertEquals(tree.getNumberSeen("3 4 4 5 0")/tree.getNumberSeen("4 5 0 4 1"),1.0,5.0);
	}
}